import * as THREE from "../../../../modules/three/three.module.js";
import Blocking from "../../model/Blocking.js";

const cubeGeometry = new THREE.BoxGeometry(1, 1, 1);
const mtl = new THREE.MeshBasicMaterial({
  color: '#185598',
  transparent: true,
  opacity: 0.3,
});
const lineMtl = new THREE.LineBasicMaterial({ color: '#001493' });
const edgeGeometry = new THREE.EdgesGeometry(cubeGeometry);

export default class BlockingMesh extends THREE.Mesh {
  /**
   * @type {Blocking}
   */
  vo;
  positionOld = new THREE.Vector3()
  rotationOld = new THREE.Euler()
  scaleOld = new THREE.Vector3()

  constructor(geometry = cubeGeometry, material = mtl) {
    super(geometry, material);
    const edges = new THREE.LineSegments(edgeGeometry, lineMtl);
    this.add(edges);
  }

  initBlocking(vo) {
    this.vo = vo;
    this.position.copy(vo.position);
    this.rotation.copy(vo.rotation);
    this.scale.copy(vo.scale);
    return this;
  }

  updateVO() {
    this.vo.position.copy(this.position);
    this.vo.rotation.copy(this.rotation);
    this.vo.scale.copy(this.scale);
  }

  backupTransform() {
    this.positionOld.copy(this.position);
    this.rotationOld.copy(this.rotation);
    this.scaleOld.copy(this.scale);
  }

  /**
   * @param {OBB} obb
   */
  initFromObb(obb) {
    if (!this.vo) {
      this.vo = new Blocking();
    }
    const vo = this.vo;
    vo.initByObb(obb)
    this.initBlocking(vo);
  }
}
